<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/utils.html">
<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/model/event_set.html">
<link rel="import" href="/tracing/model/object_collection.html">
<link rel="import" href="/tracing/model/scoped_id.html">
<link rel="import" href="/tracing/model/selection_state.html">
<link rel="import" href="/tracing/ui/timeline_viewport.html">
<link rel="import" href="/tracing/ui/tracks/drawing_container.html">
<link rel="import" href="/tracing/ui/tracks/object_instance_track.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  const EventSet = tr.model.EventSet;
  const ObjectInstanceTrack = tr.ui.tracks.ObjectInstanceTrack;
  const Viewport = tr.ui.TimelineViewport;

  const createObjects = function() {
    const objects = new tr.model.ObjectCollection({});
    const scopedId1 = new tr.model.ScopedId('ptr', '0x1000');
    objects.idWasCreated(scopedId1, 'tr.e.cc', 'Frame', 10);
    objects.addSnapshot(scopedId1, 'tr.e.cc', 'Frame', 10, 'snapshot-1');
    objects.addSnapshot(scopedId1, 'tr.e.cc', 'Frame', 25, 'snapshot-2');
    objects.addSnapshot(scopedId1, 'tr.e.cc', 'Frame', 40, 'snapshot-3');
    objects.idWasDeleted(scopedId1, 'tr.e.cc', 'Frame', 45);

    const scopedId2 = new tr.model.ScopedId('ptr', '0x1001');
    objects.idWasCreated(scopedId2, 'skia', 'Picture', 20);
    objects.addSnapshot(scopedId2, 'skia', 'Picture', 20, 'snapshot-1');
    objects.idWasDeleted(scopedId2, 'skia', 'Picture', 25);
    return objects;
  };

  test('instantiate', function() {
    const objects = createObjects();
    const frames = objects.getAllInstancesByTypeName().Frame;
    frames[0].snapshots[1].selectionState =
        tr.model.SelectionState.SELECTED;

    const div = document.createElement('div');

    const viewport = new Viewport(div);
    const drawingContainer = new tr.ui.tracks.DrawingContainer(viewport);
    Polymer.dom(div).appendChild(drawingContainer);

    const track = ObjectInstanceTrack(viewport);
    Polymer.dom(drawingContainer).appendChild(track);

    this.addHTMLOutput(div);
    drawingContainer.invalidate();

    track.heading = 'testBasic';
    track.objectInstances = frames;
    const dt = new tr.ui.TimelineDisplayTransform();
    dt.xSetWorldBounds(0, 50, track.clientWidth);
    track.viewport.setDisplayTransformImmediately(dt);
  });

  test('selectionHitTestingWithThreadTrack', function() {
    const objects = createObjects();
    const frames = objects.getAllInstancesByTypeName().Frame;

    const track = ObjectInstanceTrack(new Viewport());
    track.objectInstances = frames;

    // Hit outside range
    let selection = new EventSet();
    track.addIntersectingEventsInRangeToSelectionInWorldSpace(
        8, 8.1, 0.1, selection);
    assert.strictEqual(selection.length, 0);

    // Hit the first snapshot, via pixel-nearness.
    selection = new EventSet();
    track.addIntersectingEventsInRangeToSelectionInWorldSpace(
        9.98, 9.99, 0.1, selection);
    assert.strictEqual(selection.length, 1);
    assert.instanceOf(tr.b.getOnlyElement(selection), tr.model.ObjectSnapshot);

    // Hit the instance, between the 1st and 2nd snapshots
    selection = new EventSet();
    track.addIntersectingEventsInRangeToSelectionInWorldSpace(
        20, 20.1, 0.1, selection);
    assert.strictEqual(selection.length, 1);
    assert.instanceOf(tr.b.getOnlyElement(selection), tr.model.ObjectInstance);
  });

  test('addEventNearToProvidedEventToSelection', function() {
    const objects = createObjects();
    const frames = objects.getAllInstancesByTypeName().Frame;

    const track = ObjectInstanceTrack(new Viewport());
    track.objectInstances = frames;

    const instance = new tr.model.ObjectInstance(
        {}, new tr.model.ScopedId('ptr', '0x1000'), 'cat', 'n', 10);

    assert.doesNotThrow(function() {
      track.addEventNearToProvidedEventToSelection(instance, 0, undefined);
    });
  });
});
</script>

